//
//  AddressViewController.m
//  Products_B
//
//  Created by lanou on 16/6/1.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "AddressViewController.h"
#import "FMDBTools.h"
#import "SureDingdanViewController.h"
@interface AddressViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTf;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTf;
@property (weak, nonatomic) IBOutlet UITextField *addressTf;

@property (weak, nonatomic) IBOutlet UISwitch *swt;

@end

@implementation AddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 创建数据库表格
    [[FMDBTools shareInstance] creatTableWithTableName:@"shouhuoAddress"];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"添加收货地址";
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName]];
        
        UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CookClassBack.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButton:)];
        self.navigationItem.leftBarButtonItem = backBarButton;
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(50, kHeight - 114, kWidth - 100, 30);
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor orangeColor].CGColor;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    return self;
}

- (void)backBarButton:(UIBarButtonItem *)barButton {

    SureDingdanViewController *sureVC = self.navigationController.viewControllers[2];
    //[self.navigationController popToRootViewControllerAnimated:YES];
    [self.navigationController popToViewController: sureVC animated:YES];
}

// 保存按钮触发的方法
- (void)buttonAction:(UIButton *)button {

    if ([self.nameTf.text  isEqual: @""]) {
        [self showAlertWithTitle:@"收货人不能为空"];
        return;
    } else if ([self.phoneNumberTf.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"手机号码不能为空"];
        return;
    } else if (self.phoneNumberTf.text.length < 11) {
        [self showAlertWithTitle:@"请输入11位数字的手机号码"];
        return;
    } else if ([self.addressTf.text isEqualToString:@""]) {
        [self showAlertWithTitle:@"地址不能为空"];
        return;
    } else {
        ChuanzhiModel *model = [[ChuanzhiModel alloc] init];
        model.nameString = self.nameTf.text;
        model.phoneString = self.phoneNumberTf.text;
        model.addressString = self.addressTf.text;
        // 地址信息是否为默认
        model.isMoren = _swt.on;
        // blockc传值
        if (self.block) {
            self.block(model);
        }
        // 保存到数据库
        [[FMDBTools shareInstance] insertDataWithTableName:@"shouhuoAddress" name:self.nameTf.text phone:self.phoneNumberTf.text address:self.addressTf.text isMoren:model.isMoren];
        
        // 将开关状态存储到本地
        [[NSUserDefaults standardUserDefaults] setBool:_swt.on forKey:@"isOn"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        
        
        NSLog(@"+++++ %d",_swt.on);
        UIViewController *vc = self.navigationController.viewControllers[2];
        [self.navigationController popToViewController: vc animated:YES];
    }
}

// 当文本输入框等于11个数字时，禁止再输入
- (IBAction)textFieldAction:(UITextField *)sender {
    if (sender.text.length >= 11) {
        sender.userInteractionEnabled = NO;
    }
}

- (void)showAlertWithTitle:(NSString *)title {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *userAction = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:userAction];
    [self presentViewController:alert animated:YES completion:nil];
    [self performSelector:@selector(dissmissAlert:) withObject:alert afterDelay:1.5];
}

- (void)dissmissAlert:(UIAlertController *)alert {
    [alert dismissViewControllerAnimated:YES completion:nil];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.nameTf resignFirstResponder];
    [self.phoneNumberTf resignFirstResponder];
    [self.addressTf resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
